<template>
  <scroll class="suggest" :data="result" :pullup="pullup" :beforeScroll="beforeScroll" @beforeScroll="listScroll" @scrollToEnd="getMore" ref="suggest">
    <ul class="suggest-list">
      <li class="suggest-item" v-for="(item, index) of result" :key="index" @click="selectItem(item)">
        <div class="icon">
          <i :class="getIconCls(item)"></i>
        </div>
        <div class="name">
          <p class="text" v-html="getDisplayName(item)"></p>
        </div>
      </li>
      <loading v-show="hasMore" title=""></loading>
    </ul>
    <div class="no-result-wrapper" v-show="!hasMore && !result.length">
      <no-result title="暂无搜索结果"></no-result>
    </div>
  </scroll>
</template>

<script type='text/ecmascript-6'>
  import { search } from 'api/search'
  import { ERR_OK } from 'api/config'
  import { createSong } from 'common/js/song'
  import Scroll from 'base/scroll/scroll'
  import Loading from 'base/loading/loading'
  import Singer from 'common/js/singer'
  import { mapMutations, mapActions } from 'vuex'
  import NoResult from 'base/no-result/no-result'

  const TYPE_SINGER = 'singer'
  const perpage = 20

  export default {
    props: {
      keyword: {
        type: String,
        default: ''
      },
      showSinger: {
        type: Number,
        default: 1
      }
    },
    data () {
      return {
        page: 1,
        result: [],
        pullup: true,
        beforeScroll: true,
        hasMore: true
      }
    },
    watch: {
      keyword () {
        this._search()
      }
    },
    methods: {
      getIconCls (item) {
        return item.type === TYPE_SINGER ? 'icon-mine' : 'icon-music'
      },
      getDisplayName (item) {
        return item.type === TYPE_SINGER ? item.singername : `${item.name} - ${item.singer}`
      },
      getMore () {
        if (!this.hasMore) {
          return
        }
        this.page++
        search(this.keyword, this.page, this.showSinger, perpage).then(res => {
          if (ERR_OK === res.code) {
            console.log(res)
            this.result = this.result.concat(this._genResult(res.data))
            this._chechMore(res.data)
          }
        })
      },
      selectItem (item) {
        if (item.type === TYPE_SINGER) {
          const singer = new Singer({
            id: item.singermid,
            name: item.singername
          })
          this.$router.push({
            path: `search/${singer.id}`
          })
          this.setSinger(singer)
        } else {
          this.insertSong(item)
        }

        this.$emit('select')
      },
      listScroll () {
        this.$emit('listScroll')
      },
      refresh () {
        this.$refs.suggest.refresh()
      },
      _search () {
        this.page = 1
        this.hasMore = true
        this.$refs.suggest.scrollTo(0, 0)
        search(this.keyword, this.page, this.showSinger, perpage).then(res => {
          if (ERR_OK === res.code) {
            console.log(res)
            this.result = this._genResult(res.data)
            this._chechMore(res.data)
          }
        })
      },
      _chechMore (data) {
        const song = data.song
        if (!song.list.length || (song.curnum + song.curpage * perpage) > song.totalnum) {
          this.hasMore = false
        }
      },
      _genResult (data) {
        let ret = []
        if (data.zhida && data.zhida.singerid) {
          ret.push({ ...data.zhida, ...{ type: TYPE_SINGER } })
        }
        if (data.song) {
          ret = ret.concat(this._normalizeSong(data.song.list))
        }
        console.log(ret)
        return ret
      },
      _normalizeSong (list) {
        let ret = []
        list.forEach(musicData => {
          if (musicData.songid && musicData.albumid) {
            ret.push(createSong(musicData))
          }
        })
        return ret
      },
      ...mapMutations({
        setSinger: 'SET_SINGER'
      }),
      ...mapActions([
        'insertSong'
      ])
    },
    components: {
      Scroll,
      Loading,
      NoResult
    }
  }
</script>

<style scoped lang='stylus' rel='stylesheet/stylus'>
  @import "~common/stylus/variable"
  @import "~common/stylus/mixin"

  .suggest
    height: 100%
    overflow: hidden
    .suggest-list
      padding: 0 30px
      .suggest-item
        display: flex
        align-items: center
        padding-bottom: 20px
      .icon
        flex: 0 0 30px
        width: 30px
        [class^="icon-"]
          font-size: 14px
          color: $color-text-d
      .name
        flex: 1
        font-size: $font-size-medium
        color: $color-text-d
        overflow: hidden
        .text
          no-wrap()
    .no-result-wrapper
      position: absolute
      width: 100%
      top: 50%
      transform: translateY(-50%)
</style>
